package com.bii.gelapp;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ExportSaveDialog extends Dialog {
	public Context savecontext;
	Button btSave, btCancel;
	EditText etSave;
	Bitmap toExport;
	public ExportSaveDialog(Context context, Bitmap bmp) {
		super(context);
		try {
			this.savecontext = context;
			this.toExport = bmp;
		} catch (Exception ex) {

		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.exportsavedialog);
		WindowManager.LayoutParams wmlp = this.getWindow().getAttributes();
		WindowManager wm = (WindowManager) this.savecontext.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		wmlp.gravity = Gravity.CENTER_VERTICAL;
		wmlp.width = display.getWidth();

		btSave = (Button) findViewById(R.id.btSave);
		btCancel = (Button) findViewById(R.id.btCancel);
		etSave = (EditText) findViewById(R.id.etSave);
		etSave.setText("Picture 1");
		
		btSave.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				String pictitle = etSave.getText().toString();
				boolean stored = storeImage(toExport, pictitle);
				
				if(stored == true)
				{
					
					Toast.makeText(savecontext, "Image saved successfully", Toast.LENGTH_SHORT).show();
					dismiss();
				}
				else
				{
					
					Toast.makeText(savecontext, "Error saving image", Toast.LENGTH_SHORT).show();
					dismiss();
				}
				
			}
		});
		
		btCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();

			}
		});
		
		
		

	}
	
	private boolean storeImage(Bitmap imageData, String filename) {
		String root = Environment.getExternalStorageDirectory().toString();
		File myDir = new File(root + "/Pictures/saved_images");  
		Log.d("tag1", myDir.toString());
		myDir.mkdirs();
		String fname = filename+ ".png";
		File file = new File (myDir, fname);
		if (file.exists ()) file.delete (); 
		try {
		       FileOutputStream out = new FileOutputStream(file);
		       imageData.compress(Bitmap.CompressFormat.JPEG, 90, out);
		       out.flush();
		       out.close();
		       return true;

		} catch (Exception e) {
		       e.printStackTrace();
		       return false;
		}
		
	}
}
